package com.masterlu.module.product.controller.app.liked;

import cn.hutool.core.collection.CollUtil;
import com.masterlu.framework.common.pojo.CommonResult;
import com.masterlu.framework.common.pojo.PageResult;
import com.masterlu.framework.security.core.annotations.PreAuthenticated;
import com.masterlu.module.product.controller.app.liked.vo.AppLikedPageReqVO;
import com.masterlu.module.product.controller.app.liked.vo.AppLikedReqVO;
import com.masterlu.module.product.controller.app.liked.vo.AppLikedRespVO;
import com.masterlu.module.product.dal.dataobject.liked.ProductLikedDO;
import com.masterlu.module.product.service.liked.ProductLikedService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.masterlu.framework.common.pojo.CommonResult.success;
import static com.masterlu.framework.common.util.collection.CollectionUtils.convertList;
import static com.masterlu.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 评论点赞")
@RestController
@RequestMapping("/product/liked")
public class AppLikedController {

    @Resource
    private ProductLikedService productLikedService;
/*    @Resource
    private ProductCommentService productCommentService;*/

    @PostMapping(value = "/create")
    @Operation(summary = "添加点赞")
    @PreAuthenticated
    public CommonResult<Long> createLiked(@RequestBody @Valid AppLikedReqVO reqVO) {
        return success(productLikedService.createLiked(getLoginUserId(), reqVO.getCommentId()));
    }
    @PostMapping(value = "/createOrUpdate")
    @Operation(summary = "添加点赞")
    @PreAuthenticated
    public CommonResult<Long> createOrUpadateLiked(@RequestBody @Valid AppLikedReqVO reqVO) {

        return success(productLikedService.createOrUpdateLiked(getLoginUserId(), reqVO.getCommentId(),reqVO.getLiked()));
    }

    @DeleteMapping(value = "/delete")
    @Operation(summary = "取消单个点赞")
    @PreAuthenticated
    public CommonResult<Boolean> deleteLiked(@RequestBody @Valid AppLikedReqVO reqVO) {
        productLikedService.deleteLiked(getLoginUserId(), reqVO.getCommentId());
        return success(Boolean.TRUE);
    }

    @GetMapping(value = "/page")
    @Operation(summary = "获得点赞分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppLikedRespVO>> getLikedPage(AppLikedPageReqVO reqVO) {
        PageResult<ProductLikedDO> LikedPage = productLikedService.getLikedPage(getLoginUserId(), reqVO);
        if (CollUtil.isEmpty(LikedPage.getList())) {
            return success(PageResult.empty());
        }

        // 得到商品 Comment 信息
        List<ProductLikedDO> Likeds = LikedPage.getList();
        List<Long> CommentIds = convertList(Likeds, ProductLikedDO::getCommentId);
        /*TODO:  lujisen*/
  /*      List<ProductCommentDO> Comments = productCommentService.getCommentList(CommentIds);*/

        // 转换 VO 结果
        PageResult<AppLikedRespVO> pageResult = new PageResult<>(LikedPage.getTotal());
      /*  pageResult.setList(ProductLikedConvert.INSTANCE.convertList(Likeds, Comments));*/
        return success(pageResult);
    }

    @GetMapping(value = "/exits")
    @Operation(summary = "检查是否收藏过评论")
    @PreAuthenticated
    public CommonResult<Boolean> isLikedExists(AppLikedReqVO reqVO) {
        ProductLikedDO Liked = productLikedService.getLiked(getLoginUserId(), reqVO.getCommentId());
        return success(Liked != null);
    }

    @GetMapping(value = "/get-count")
    @Operation(summary = "获得评论数量")
    @PreAuthenticated
    public CommonResult<Long> getLikedCount() {
        return success(productLikedService.getLikedCount(getLoginUserId()));
    }

}
